/**
 * @author 汪孟苹
 */
define(['jquery','template','scprit/department/view/template/oxygenPulseFequency.tpl.js'],function($,template,tpl){
	var oxygen = {};
	var drawoxygenList = function(data){
		return template("tpl_oxygen_list",data);
	}
	var drawoxygenListDay = function(data){
		return template("tpl_oxygen_listDay",data);
	}
	var drawUserinfo = function(data){
		return template("tpl_userinfo",data);
	}
	var drawoxygenDetailInfo = function(data){
		return template("tpl_oxygen_datailInfo",data);
	}
	var oxygenListData = function(list){
		var arr = [];
		for(var i=0;i<list.length;i++){
			arr[i] = list[i];
		}
		return arr;
	}
	var oxygenDetailInfoData = function(list){
		var arr = [];
		var minute = parseInt(list.timeLong / 60);
		var second = parseInt(list.timeLong % 60);
		list.timeLong = minute+'分'+second+'秒';
		list.userType = list.userType=="01"? "早产儿":"正常";
		var time1 = list.testTime.split(' ')[0];
		var mouth = time1.split('-')[1];
		var day = time1.split('-')[2];
		var time2 = list.testTime.split(' ')[1];
		var hour = time2.split(':')[0];
		var minute = time2.split(':')[1];
		list.testTime = mouth+"月"+day+"日 "+hour+":"+minute;
		arr.push(list);
		return arr;
	}
	oxygen.arrOxygenPerHandle= function(arrOxygenPer,timeLong){
		var arr = [];
		var arrLabel = ['94%~100%','90%~93%','85%~89%','80%~84%','0%~79%','正常','异常'];
		var arrDots = ['  ...........    ','    ...........    ','    ...........    ','    ...........    ','      ...........    ','             ............    ','             ............    '];
		var arrPercent = [];
		var arrtimeLong = timeLong.split("分");
		var minute = arrtimeLong[0];
		var second = arrtimeLong[1].split("秒")[0];
		var timeLong = parseInt(minute)*60 + parseInt(second);
		for (var i = 0; i < arrLabel.length; i++) {
			for (var j = 0; j < arrDots.length; j++) {
				if(i == j){
					arrLabel[i] = arrLabel[i] + arrDots[j];
				}
			}
		}
        for (var i = 0; i < arrOxygenPer.length; i++) {
        	if(arrOxygenPer[i] == null || arrOxygenPer[i] ==''){
        		arrOxygenPer[i] = 0;
        	}
        	arrPercent[i] = '  / ' + Math.round(arrOxygenPer[i] / timeLong *10000) / 100.00 + "%";
            arr[i] = arrOxygenPer[i];
            var minute = parseInt(arr[i] / 60);
            var second = parseInt(arr[i] % 60);
            arr[i] = minute+"'"+second;
            for (var j = 0; j < arrLabel.length; j++) {
            	if(i == j){
            		arr[i] = arrLabel[j] + arr[i];
            	}
            }
            arr[i] = arr[i] + arrPercent[i];
        }
        return arr;
	}
	oxygen.lineData = function(list){
		var arrPulseRate = [];
        var arrOxygensaturation = [];
        var arrTime = [];
        for (var i = 0; i < list.length; i++) {
            arrPulseRate.push(list[i].pulseRate);
            arrOxygensaturation.push(list[i].oxygensaturation);
            arrTime.push(list[i].testTime.substr(11));
        }
        return {arrPulseRate:arrPulseRate,arrOxygensaturation:arrOxygensaturation,arrTime:arrTime};
	}
	oxygen.timeLong=function(timeLong){
		var d=parseInt(timeLong/(3600*24));
		var h=parseInt(timeLong/3600);
		var m=parseInt((timeLong%3600)/60);
		var s=parseInt((timeLong%3600)%60);
		var duration;
		if(d>=1){
			if(h>=1){
				if(m>=1){
					if(s>=1){
						duration=d+"天"+h+"小时"+m+"分"+s+"秒";
					}else{
						duration=d+"天"+h+"小时"+m+"分";
					}
				}else{
					if(s>=1){
						duration=d+"天"+h+"小时"+s+"秒";
					}else{
						duration=d+"天"+h+"小时";
					}
				}
			}else{
				if(m>=1){
					if(s>=1){
						duration=d+"天"+m+"分"+s+"秒";
					}else{
						duration=d+"天"+m+"分";
					}
				}else{
					if(s>=1){
						duration=d+"天"+s+"秒";
					}else{
						duration=d+"天";
					}
				}
			}
		}else{
			if(h>=1){
				if(m>=1){
					if(s>=1){
						duration=h+"小时"+m+"分"+s+"秒";
					}else{
						duration=h+"小时"+m+"分";
					}
				}else{
					if(s>=1){
						duration=h+"小时"+s+"秒";
					}else{
						duration=h+"小时";
					}
				}
			}else{
				if(m>=1){
					if(s>=1){
						duration=m+"分"+s+"秒";
					}else{
						duration=m+"分";
					}
				}else{
					if(s>=1){
						duration=s+"秒";
					}else{
						duration="暂无数据";
					}
				}
			}
		}
		return duration;
	}
	oxygen.listInit = function(options){
		var data = {
			rows: oxygenListData(options.list)
		}
		$("#"+options.id).html(tpl.init1());
		var h = drawoxygenList(data);
		$("#"+options.id).html(h);
	}
	oxygen.DetailInfoInit = function(options){
		var data = {
			rows: oxygenDetailInfoData(options.list)
		}
		$("#"+options.id).html(tpl.init2());
		var h = drawoxygenDetailInfo(data);
		$("#"+options.id).html(h);
	}
	oxygen.listDayInit = function(options){
		var data = {
			rows: oxygenListData(options.list)
		}
		$("#"+options.id).html(tpl.init3());
		var h = drawoxygenListDay(data);
		$("#"+options.id).html(h);
	}
	oxygen.userInfoInit = function(options){
		$("#"+options.id).html(tpl.init4());
		var h = drawUserinfo(options.list);
		$("#"+options.id).html(h);
	}
	return oxygen;
});